<?php

/**
 * Controller_keep
 教师档案
 */

class Controller_keep extends MyApp
{


	public function __construct()
	{
		parent::__construct();
		$this->model->excel 	= YOU::loadModel('excel');
		$this->model->semester 	= YOU::loadModel('semester');
		$this->model->record 	= YOU::loadModel('record');
		$this->model->user 		= YOU::loadModel('user');
		$this->model->file    	= YOU::loadModel('file');

		$type = $this->get->type;
		if (!$type) {
			$type = 1;
		}
		$this->tpl->assign('type', $type);
		$this->type = $type;
	}

	/** 首页 **/
	public function actionIndex()
	{

	}

	/** 列表 **/
	public function actionList()
	{
		$keep_type = (int)$this->type;
		//按条件搜索
		$where = "where keep_type = " . $keep_type;

		if ($_POST) {
			//var_dump($keep_type);exit;
			if($this->post->keep_name){
				$where .= " AND keep_name like '%" .$this->post->keep_name. "%' " ;
				$this->tpl->assign('keep_name',$this->post->keep_name);
			}
			if($this->post->keep_owner){

				$teacher_lists = $this->db->fetchAll("SELECT staff_id FROM `tbl_staff` where staff_name like '%" . $this->post->keep_owner. "%' ");

				$teachers = '';
				foreach ($teacher_lists as $key => $value){
					$teachers[$key] = $value['staff_id'];
				}
				if(!empty($teachers)){
					$staff = implode(',',$teachers);
					$where .= " AND keep_owner in (" . $staff  .") ";
				}else{return false;}
				$this->tpl->assign('keep_owner',$this->post->keep_owner);
			}
			$from = ($this->post->from);
			$to = ($this->post->to);
			if (!empty($from)) {
				$where .= " AND keep_time >= '{$from}' ";
				$this->tpl->assign("from", $from);
			}
			if (!empty($to)) {
				$where .= " AND keep_time <= '{$to}' ";
				$this->tpl->assign("to", $to);
			}
		}

		

		$keep_lists = $this->db->fetchAll("SELECT * FROM `tbl_keep` {$where}  ORDER BY keep_id DESC ");

		foreach ($keep_lists as $key => $value){
			if(!empty($value['keep_owner'])) {
				$teacher_lists = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id = " . $value['keep_owner']);
				$keep_lists[$key]['keep_owner'] = $teacher_lists['staff_name'];
			}
			if(!empty($value['keep_remark'])) {
				$keep_lists[$key]['keep_remark'] = mb_substr($value['keep_remark'],0,7,'utf-8');
			}
			// 提交者
			$keep_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);
		
		}

		$this->tpl->assign("keep_lists", $keep_lists);
		$this->tpl->assign("count", $count);
		$this->tpl->assign("keep_type", $keep_type);

		$this->tpl->display("keep/m.keep.list.tpl.php");
	}


	/** 添加 **/
	public function actionAdd()
	{
		$keep_type   = $this->get->keep_type;
		
		if ($_POST) {
			$user_id = $this->user['user_id'];
			$sttaf = $this->model->user->getInfo($user_id,'type_id');
			$sttaf_id = $sttaf['type_id'];
			$time = date("Y-m-d h:i:s");
			$keep_name   = $this->post->keep_name;
			$keep_unit   = $this->post->keep_unit;
			$keep_time   = $this->post->keep_time ? $this->post->keep_time : $time;
			$keep_remark = $this->post->keep_remark;
			$keep_hour   = $this->post->keep_hour;
			$keep_min    = $this->post->keep_min;
			$keep_score  = $this->post->keep_score;
			$keep_content= $this->post->keep_content;
			$staff_id    = $sttaf_id;

			// 写入数据库
			$info = array(
			"keep_name"   => $keep_name,
			"keep_unit"   => $keep_unit,
			"keep_hour"   => $keep_hour,
			"keep_min"    => $keep_min,
			"keep_owner"  => $staff_id,
			"keep_time"   => $keep_time,
			"keep_remark" => $keep_remark,
			"keep_content"=> $keep_content,
			"keep_score"  => $keep_score,
			"keep_type"   => $keep_type,
			"user_id"     => $this->user['user_id'],
			"status"      => 1,
			"create_time" => date("Y-m-d H:i:s"),
			"update_time" => "0000-00-00 00:00:00",
			);

			$this->db->do_insert("tbl_keep", $info);
			$keep_id = $this->db->insert_id();
			// 附件上传

			$file = $this->model->file->upload("keep_file", $keep_id, array("txt","ppt", "xls", "xlsx", "docs", "docx","doc", "pdf", "jpg", "png", "gif", "zip", "rar"));
			if (!empty($file)){
				$info = array(
				"keep_file" =>  $file['fname'],
				);

				$this->db->do_update("tbl_keep", $info, "keep_id='{$keep_id}'");
			}

			// 跳转
			$type = $this->type;
			Helper_Client::location("添加成功", "index.php?a=keep.list&type=$type");
		}
		$teachers = $this->db->fetchAll("SELECT `staff_id`,`staff_name` FROM `tbl_staff` ");

		$this->tpl->assign('teachers', $teachers);

		$this->tpl->display("keep/m.keep.add.tpl.php");
	}

	/** 修改 **/
	public function actionUpdate()
	{
		$keep_id = (int)$this->get->keep_id;
		$data = $this->db->fetchRow("SELECT * FROM `tbl_keep` WHERE keep_id='{$keep_id}'");
		if (empty($data)) {
			Helper_Client::alert("该奖不存在");
		}

		if ($_POST) {
			$user_id = $this->user['user_id'];
			$sttaf = $this->model->user->getInfo($user_id,'type_id');
			$sttaf_id = $sttaf['type_id'];
			$time = date("Y-m-d h:i:s");
			$keep_name   = $this->post->keep_name;
			$keep_unit   = $this->post->keep_unit;
			$keep_owner  = $this->post->keep_owner ? $this->post->keep_owner : $sttaf_id;
			$keep_time   = $this->post->keep_time ? $this->post->keep_time : $time;
			$keep_remark = $this->post->keep_remark;
			$keep_hour   = $this->post->keep_hour;
			$keep_min    = $this->post->keep_min;
			$keep_score  = $this->post->keep_score;



			// 写入数据库
			$info = array(
			"keep_name"   => $keep_name,
			"keep_unit"   => $keep_unit,
			"keep_hour"   => $keep_hour,
			"keep_min"    => $keep_min,
			"keep_owner"  => $keep_owner,
			"keep_time"   => $keep_time,
			"keep_remark" => $keep_remark,
			"keep_score"  => $keep_score,

			);
			$this->db->do_update("tbl_keep", $info, "keep_id='{$keep_id}'");
			// 附件上传
			if ($this->post->keep_file_re=='1'){
				// 附件上传
				$file = $this->model->file->upload("keep_file", $keep_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));

				if (!empty($file)){
					$info = array(
					"keep_file" => $file['fname'],
					);

					$this->db->do_update("tbl_keep", $info, "keep_id='{$keep_id}'");
				}
			}

			// 跳转
			$type = $this->type;
			Helper_Client::location("修改成功", "index.php?a=keep.list&type=$type");

		}
		$teachers = $this->db->fetchAll("SELECT `staff_id`,`staff_name` FROM `tbl_staff` ");
		$this->tpl->assign('teachers', $teachers);

		$this->tpl->assign("data", $data);
		$this->tpl->display("keep/m.keep.update.tpl.php");
	}

	/** 删除 **/
	public function actionDel()
	{
		$keep_id = (int)$this->get->keep_id;
		$this->db->query("DELETE  FROM `tbl_keep` WHERE keep_id='{$keep_id}'");
		Helper_Client::location("删除成功", "index.php?a=keep.list");
	}

	//------------------------------------------------------------------------------//
	/** 查看**/
	public function actionInfo()
	{
		$keep_id = (int)$this->get->keep_id;
		$data = $this->db->fetchRow("SELECT * FROM `tbl_keep` WHERE keep_id='{$keep_id}'");
		if (empty($data)) {
			Helper_Client::alert("该奖不存在");
		}
		$teacher_data = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id = ".$data['keep_owner']);
		$data['keep_owner']       = $teacher_data['staff_name'];
		//        $prize_lists = $this->db->fetchRow("SELECT `prize_name` FROM `tbl_prize` WHERE prize_id = ".$data['keep_rank']);
		//        $data['keep_rank']       = $prize_lists['prize_name'];
		//        $data['keep_file'] = UPLOAD_PATH.'/'.$data['keep_file'];
		//        $teach_lists = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id = ".$data['keep_teach']);
		//        $data['keep_teach']       = $teach_lists['staff_name'];
		//        if($this->type != 5 && $this->type != 7){//获奖者姓名转换
		//            $teacher_lists = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id = ".$data['keep_owner']);
		//            $data['keep_owner']       = $teacher_lists['staff_name'];
		//    }

		$this->tpl->assign("data", $data);
		$this->tpl->display("keep/m.keep.info.tpl.php");
	}
	/** 下载**/
	public function actionoutput(){
		$fname = $this->get->keep_file;
		$fpath = UPLOAD_PATH."/keep_file/{$fname}";

		// 输出下载文件头s
		header("Content-Disposition: attachment; filename={$fname}");
		// 在线输出
		readfile($fpath);

	}


}

?>